package br.com.quiz.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.FacesConverter;
import br.com.quiz.dao.DisciplinaDAO;
import br.com.quiz.domain.Disciplina;

@FacesConverter("disciplinaConverter")
public class DisciplinaConverter implements javax.faces.convert.Converter {

	@Override
	public Object getAsObject(FacesContext facesContext, UIComponent component,
			String valor) {
		try {
			Integer codigo = Integer.parseInt(valor);
			Disciplina disciplina = DisciplinaDAO.filtrarPorId(codigo);
			return disciplina;
		} catch (RuntimeException ex) {
			return null;
		}
	}

	@Override
	public String getAsString(FacesContext facesContext, UIComponent component,
			Object obj) {
		// TODO Auto-generated method stub
		try {
			Disciplina disciplina = (Disciplina) obj;
			Integer codigo = disciplina.getDisId();
			return codigo.toString();
		} catch (RuntimeException ex) {
			return null;
		}
	}

}
